<?php
/*
Plugin Name: Login As...
Description: Allows site administrator to log in as any user by specifying their user login name
Version: 1.1
Author: Vince Ng
*/

add_action('admin_menu', 'addLoginAsMenu');

function addLoginAsMenu()
{	
	if (is_site_admin())
	{
		global $error;

		if($_POST['user'])
		{
			require_once(ABSPATH . WPINC . '/pluggable.php');

			$userData = get_userdatabylogin($_POST['user']);

			if($userData)
			{
				wp_setcookie($userData->user_login, md5($userData->user_pass), true, '', '');
				wp_redirect(get_active_blog_for_user($userData->ID)->siteurl . "/wp-admin/");
				exit();
			}
			else
				$error = TRUE;
		}

		add_submenu_page('wpmu-admin.php', 'Login As...', 'Login As...', 10, 'loginAs', 'displayLoginAsPage');		
	}
}

function displayLoginAsPage()
{
	global $error;

	if($error)
		print('<div id="message" class="updated fade"><p>' . $_POST['user'] . ' does not exist!</p></div>');
?>
	<div class="wrap">
		<h2>Login As...</h2>
		<p>Type in the user name to log in as that user.  You will be logged out and redirected to their dashboard.</p>
		<form action="" method="post">
			<input type="text" name="user" id="user" size="25" /><br />
			<input class="button" type="submit" name="submit" value="Login" />
		</form>
	</div>
<?php
}
?>
